package com.ooqiu.gaming.components.storage;

import android.content.Context;

import com.ooqiu.gaming.common.Constants;
import com.ooqiu.gaming.common.entities.GamingUser;
import com.ooqiu.gaming.ui.web.X5WebView;
import com.ooqiu.gaming.utils.LocalBroadcastManagerUtil;
import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.WebStorage;
import com.umeng.analytics.MobclickAgent;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Eenie
 * @date 2017/11/21
 */

public class UserStorage {

    private Context mContext;
    private GamingUser mUser;

    public UserStorage(Context context) {
        this.mContext = context;
        mUser = recoverUser();
    }

    private GamingUser recoverUser() {
        try {
            FileInputStream fos = mContext.openFileInput(Constants.USER_FILE_NAME);
            ObjectInputStream oinput = null;
            oinput = new ObjectInputStream(fos);
            GamingUser user = (GamingUser) oinput.readObject();
            fos.close();
            oinput.close();
            return user;
        } catch (Exception e) {

        }
        return null;
    }


    public GamingUser getUser() {

        return mUser;
    }

    public void saveUser(GamingUser user) {
        saveUser(user, true);
    }

    public void saveUser(GamingUser user, boolean needNotice) {

        if (user != null && user.getUrl() != null) {
            for (String url : user.getUrl()) {
                new X5WebView(mContext).loadUrl(url);






            }
        }

        if (needNotice) {
            LocalBroadcastManagerUtil.post(mContext, Constants.ACTION_LOGIN_STATE_CHANGE);
        }
        try {
            FileOutputStream fos = mContext.openFileOutput(Constants.USER_FILE_NAME, Context.MODE_PRIVATE);
            ObjectOutputStream oout = null;
            oout = new ObjectOutputStream(fos);
            oout.writeObject(user);
            fos.close();
            oout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mUser = recoverUser();
    }


    public void logout(boolean needNotice) {

        if (needNotice) {
            LocalBroadcastManagerUtil.post(mContext, Constants.ACTION_LOGIN_STATE_CHANGE);
        }

        saveUser(null, needNotice);
        MobclickAgent.onProfileSignOff();
        mUser = recoverUser();
        CookieManager.getInstance().removeAllCookie();
        WebStorage.getInstance().deleteAllData();
    }

    public void logout() {
        logout(false);
    }


    public boolean isLogin() {
        return mUser != null;
    }

    public String getToken() {

//        GamingApi.class.getMethods();
//
//        Method[] methods = GamingApi.class.getMethods(); //可以获取私有方法和公有方法, getMethods() 获取公有方法
//        Method meth = null;
//        for(Method method : methods){
//            if(method.getName().equals("test")){
//                meth = method;
//                break;
//            }
//        }
//        Annotation annotation = meth.getAnnotations()[0];

//        System.out.println(mode.value());


        return isLogin() ? mUser.getToken() : "";
    }

}
